package com.imodia.checkoo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hsq.commons.util.TreeUtil;
import com.hsq.commons.util.db.GenericIbatisDao;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.db.annotation.SqlSpace;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsPageBean;
import com.imodia.checkoo.service.CmsPageService;


@SqlSpace(spaceName = "cmspage")
@Service("cmsPageService")
public class CmsPageServiceImpl extends GenericIbatisDao<CmsPageBean, Integer> implements CmsPageService {

	private static final Log log = LogFactory.getLog(CmsPageServiceImpl.class);

	public List<TreeNodeBean> getTreePage() {
		List<TreeNodeBean> treeList = new ArrayList<TreeNodeBean>();
		List<CmsPageBean> list = list(new SqlParam());
		for (int i = 0; i < list.size(); i++) {
			CmsPageBean info = list.get(i);
			TreeNodeBean tree = new TreeNodeBean();
			tree.setNodeId(String.valueOf(info.getId()));
			tree.setNodeName(info.getCnName());
			tree.setNodePId(String.valueOf(info.getParentId()));
			tree.setPriority(info.getPriority());
			tree.setStatus(info.getIsValidate());
			tree.setObj(info);
			treeList.add(tree);
		}
		return TreeUtil.getTree(treeList, 0);
	}

	/**
	 * showType = 
	 * 
	 * @param siteId
	 * @param showType
	 * @return
	 */
	public List<TreeNodeBean> getTreePage2(int siteId, int parentId,int showType) {
		List<TreeNodeBean> treeList = new ArrayList<TreeNodeBean>();
		SqlParam sql = new SqlParam();
		if (siteId > 0) {
			sql.andEqualTo("site_id", String.valueOf(siteId));
		}
		if (parentId > 0) {
			sql.andEqualTo("parent_id", String.valueOf(parentId));
		}
		if (showType > 0) {
			sql.andEqualTo("show_type", String.valueOf(showType));
		}
		List<CmsPageBean> list = list(sql);
		for (int i = 0; i < list.size(); i++) {
			CmsPageBean info = list.get(i);
			TreeNodeBean tree = new TreeNodeBean();
			tree.setNodeId(String.valueOf(info.getId()));
			tree.setNodeName(info.getCnName());
			tree.setNodePId(String.valueOf(info.getParentId()));
			tree.setPriority(info.getPriority());
			tree.setStatus(info.getIsValidate());
			tree.setObj(info);
			treeList.add(tree);
		}
		return TreeUtil.getTree(treeList, parentId);
	}
	
	public List<TreeNodeBean> covertTreePage(List<CmsPageBean> list,int parentId) {
		List<TreeNodeBean> treeList = new ArrayList<TreeNodeBean>();
		for (int i = 0; i < list.size(); i++) {
			CmsPageBean info = list.get(i);
			TreeNodeBean tree = new TreeNodeBean();
			tree.setNodeId(String.valueOf(info.getId()));
			tree.setNodeName(info.getCnName());
			tree.setNodePId(String.valueOf(info.getParentId()));
			tree.setPriority(info.getPriority());
			tree.setStatus(info.getIsValidate());
			tree.setObj(info);
			treeList.add(tree);
		}
		return TreeUtil.getTree(treeList, parentId);
	}

	
	/**
	 * showType = 2��ʾ��3����
	 * 
	 * @param siteId
	 * @param showType
	 * @return
	 */
	public List<TreeNodeBean> getTreePage3(int siteId, int id,int showType) {
		List<TreeNodeBean> treeList = new ArrayList<TreeNodeBean>();
		SqlParam sql = new SqlParam();
		if (siteId > 0) {
			sql.andEqualTo("site_id", String.valueOf(siteId));
		}

		if (showType > 0) {
			sql.andEqualTo("show_type", String.valueOf(showType));
		}
		List<CmsPageBean> list = list(sql);
		for (int i = 0; i < list.size(); i++) {
			CmsPageBean info = list.get(i);
			TreeNodeBean tree = new TreeNodeBean();
			tree.setNodeId(String.valueOf(info.getId()));
			tree.setNodeName(info.getCnName());
			tree.setNodePId(String.valueOf(info.getParentId()));
			tree.setPriority(info.getPriority());
			tree.setStatus(info.getIsValidate());
			tree.setObj(info);
			treeList.add(tree);
		}
		int rootId = TreeUtil.findRoot(treeList, id);
		System.out.println("___id:"+id);
		System.out.println("___rootId:"+rootId);
		List<TreeNodeBean> childList = TreeUtil.findAllChild(treeList, rootId);
		System.out.println("___childList.size():"+childList.size());
		return TreeUtil.getTree(childList, 0);
	}
	
	
	public List<CmsPageBean> getByCataId(int cataId){
		String sql = "select p.* from cms_page_ref_columns pc,cms_page p where p.id = pc.page_id and pc.catalogue_id = "+cataId;
		return this.getSqlDao().list(sql, CmsPageBean.class);
	}
	
	
	public List<CmsPageBean> list(int siteId, int parentId,int showType,int isValidate) {
		SqlParam sql = new SqlParam();
		if (siteId > 0) {
			sql.andEqualTo("site_id", String.valueOf(siteId));
		}
		if (parentId !=-1) {
			sql.andEqualTo("parent_id", String.valueOf(parentId));
		}
		if (showType > 0) {
			sql.andEqualTo("show_type", String.valueOf(showType));
		}
		if (isValidate !=-1) {
			sql.andEqualTo("is_validate", String.valueOf(isValidate));
		}
		
		sql.andOrderListValue("priority", "");
		return list(sql);
	}
	
}